package control;

import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.Map;

import org.apache.commons.io.FileUtils;

import service.AdminService;
import service.FansService;
import service.ItemService;
import util.Resourse;
import util.Test2;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

import dao.LogDao;
import domain.FanUsers;
import domain.User;
import domain.UserBean;

public class AdminAction extends ActionSupport{
	private LogDao  logDao;
	private User user;
	private AdminService adminService;
	private File upload;
	private int userId;
	private String uploadFileName;
	private FansService fansService;
	private String sellOrBuy;
	private ItemService itemService;
	private String email;
	private UserBean userbean;
	private String mess;
	private String rid;
	public int getUserId() {
		return userId;
	}

	public void setUserId(int userId) {
		this.userId = userId;
	}

	public String getUploadFileName() {
		return uploadFileName;
	}

	public void setUploadFileName(String uploadFileName) {
		this.uploadFileName = uploadFileName;
	}
	
	public File getUpload() {
		return upload;
	}
	
	public void setUpload(File upload) {
		this.upload = upload;
	}
	
	
	public AdminService getAdminService() {
		return adminService;
	}

	public void setAdminService(AdminService adminService) {
		this.adminService = adminService;
	}

	public User getUser() {
		return user;
	}

	public void setUser(User user) {
		this.user = user;
	}
	public String showPersonUser(){
//		System.out.println("展示个人成功");
		User personUser = adminService.showPersonUser(userId);
		Map request = (Map)	ActionContext.getContext().get("request");
		List<FanUsers> listFaned = getFansService().showPersonFaned(userId);
		List<FanUsers> listFans = getFansService().showPersonFans(userId);
		request.put("loginDate", logDao.getDate(personUser));
		request.put("personUser", personUser);
		request.put("fanedNum", listFaned.size());
		request.put("fansNum", listFans.size());
		return SUCCESS;
	}
	
	public String showPersonUserFaned(){
		List<FanUsers> list = getFansService().showPersonFaned(userId);
		Map request = (Map)	ActionContext.getContext().get("request");
		request.put("fans", list);
		return SUCCESS;
	}
	
	public String updatePass(){
		mess = adminService.updatePass(userbean);
		if("成功".equals(mess)){
			rid = "1";
		}
		else{
			rid = "0";
		}
		return SUCCESS;
	}
	
	public String showPersonItem(){
		List list = itemService.getPersonItem(userId,sellOrBuy);
		Map request = (Map)	ActionContext.getContext().get("request");
		Map session = ActionContext.getContext().getSession();
		session.put("temp", null);
		request.put("list", list);
		return SUCCESS;
	}
	
	public String sendEmail(){
		Test2.send(email);
		return SUCCESS;
	}
	public String showPersonUserFans(){
		System.out.println("展示粉丝初步成功");
		List<FanUsers> list = getFansService().showPersonFans(userId);
		Map request = (Map)	ActionContext.getContext().get("request");
		request.put("fans", list);
		return SUCCESS;
	}
	public String insertAdmin(){
		// TODO Auto-generated method stub
//		String path ="e:/image/user"; //ServletActionContext.getServletContext().getRealPath("/images");
		String absolutePath = Resourse.basePath+ Resourse.path+Resourse.pathUser;
		String relativepath = Resourse.path+Resourse.pathUser;
		File file = new File(absolutePath);
		if(!file.exists()){
			file.mkdir();
		}
		try {
			FileUtils.copyFile(upload, new File(file,user.getAccount()+"."+uploadFileName.split("\\.")[1]));
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		if(adminService.register(user.getAccount())==0){
			adminService.insertAdmin(user,relativepath+"/"+user.getAccount()+"."+uploadFileName.split("\\.")[1]);
			return SUCCESS;
		}else {
			return ERROR;
		}
		
	}
	
	public String login(){
		
		if(adminService.login(user.getAccount(),user.getPassword())==0){
//			adminService.insertAdmin(user);
			return ERROR;
			
		}else {
			Map session = ActionContext.getContext().getSession();
			session.put("acc", user.getAccount());
//			System.out.println("�˻�"+user.getAccount());
			List<List> ll=  adminService.showIndex();
			Map s = (Map)	ActionContext.getContext().getSession();
			s.put("fanedTops", ll.get(0));
			s.put("items", ll.get(1));
			s.put("regs", ll.get(2));
			s.put("loginLog",ll.get(3));
			return SUCCESS;
		}
		
	}

	public FansService getFansService() {
		return fansService;
	}

	public void setFansService(FansService fansService) {
		this.fansService = fansService;
	}

	public String getSellOrBuy() {
		return sellOrBuy;
	}

	public void setSellOrBuy(String sellOrBuy) {
		this.sellOrBuy = sellOrBuy;
	}

	public ItemService getItemService() {
		return itemService;
	}

	public void setItemService(ItemService itemService) {
		this.itemService = itemService;
	}

	public UserBean getUserbean() {
		return userbean;
	}

	public void setUserbean(UserBean userbean) {
		this.userbean = userbean;
	}

	public String getMess() {
		return mess;
	}

	public void setMess(String mess) {
		this.mess = mess;
	}

	public String getRid() {
		return rid;
	}

	public void setRid(String rid) {
		this.rid = rid;
	}

	public LogDao getLogDao() {
		return logDao;
	}

	public void setLogDao(LogDao logDao) {
		this.logDao = logDao;
	}
}
